﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace dgmobile
{
  public partial class Form2 : Form
  {
    private int type;

    public Form2(int type)
    {
      InitializeComponent();
      //Seed for form morph. Dat tongue twist.
      this.type = type;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
      //The morphing!
      switch(type)
      {
        case 0:
          this.Text = "Atendimento externo";
          panel1.Location = new Point(12, 12);
          panel1.Visible = true;
          this.Size = new Size(269, 275);
          break;

        case 1:
          this.Text = "Procedimento de Melhoria";
          panel2.Location = new Point(12, 12);
          panel2.Visible = true;
          this.Size = new Size(266, 225);
          break;

        case 2:
          this.Text = "Tratamento de BD";
          panel3.Location = new Point(12, 12);
          panel3.Visible = true;
          this.Size = new Size(271, 333);
          break;

        case 3:
          this.Text = "Tratamento de OS";
          panel4.Location = new Point(12, 12);
          panel4.Visible = true;
          this.Size = new Size(271, 276);
          break;

        default:
          this.Text = "-";
          break;
      }

      //The resetting!
      comboBox1Tipo.SelectedIndex = 0;
      comboBox2Tipo.SelectedIndex = 0;
      comboBox3Posto.SelectedIndex = 0;
      comboBox3Retencao.SelectedIndex = 0;
      comboBox4Origem.SelectedIndex = 0;
      comboBox4Tipo.SelectedIndex = 0;
      radioButton3DG.Checked = true;
    }

    /// <summary>
    /// Creates the data object for proper DB entry.
    /// </summary>
    /// <param name="p"></param>
    private void CreateData(int p)
    {
      string type = "";
      string terminal = "";
      string station = "";
      string meta = "";
      string responsible = "";
      string status = "";
      string observation = "";

      switch(p)
      {
        case 1:
          type = "Atendimento";
          terminal = maskedTextBox1Terminal.Text;
          station = textBox1Estacao.Text;
          meta = comboBox1Tipo.Text;
          responsible = textBox1Matricula.Text;
          status = "OK";
          observation = textBox1Obs.Text;
          break;

        case 2:
          type = "Melhoria";
          terminal = "-";
          station = textBox2Estacao.Text;
          meta = comboBox2Tipo.Text;
          responsible = Config.Operator.Chapa;
          status = "OK";
          observation = textBox2Obs.Text;
          break;

        case 3:
          type = "Tratamento de BD";
          terminal = maskedTextBox3Terminal.Text;
          station = textBox3Estacao.Text;
          meta = comboBox3Retencao.Text;
          responsible = textBox3Matricula.Text;
          status = (radioButton3DG.Checked) ? "Encerrado no DG" : "Tramitado para " + comboBox3Posto.Text;
          observation = textBox3Obs.Text;
          break;

        case 4:
          type = "Tratamento de OS";
          terminal = maskedTextBox4Terminal.Text;
          station = textBox4Estacao.Text;
          meta = comboBox4Tipo.Text + " (" + comboBox4Origem.Text + ")";
          responsible = textBox4Matricula.Text;
          status = "OK";
          observation = textBox4Obs.Text;
          break;

        default:
          break;
      }

      DB.InsertData(new Data
      {
        UserID = Config.Operator.ID,
        Timestamp = DateTime.Now,
        Type = type,
        Terminal = terminal,
        Station = station,
        Meta = meta,
        Responsible = responsible,
        Status = status,
        Observation = observation
      });
    }

    //Sleek combo box enabling/disabling based on radio button activity on panel 3.
    private void radioButton3DG_CheckedChanged(object sender, EventArgs e)
    {
      comboBox3Posto.SelectedIndex = 0;
      if(radioButton3Tramitado.Checked)
      {
        comboBox3Posto.Enabled = true;
      }
      else
      {
        comboBox3Posto.Enabled = false;
      }
    }

    //Auto moves caret on masked text box to start.
    private void maskedTextBox1Terminal_Enter(object sender, EventArgs e)
    {
      this.BeginInvoke((MethodInvoker)delegate()
      {
        maskedTextBox1Terminal.Select(0, 0);
      });
    }

    //Auto moves caret on masked text box to start 2: Caret vengeance.
    private void maskedTextBox3Terminal_Enter(object sender, EventArgs e)
    {
      this.BeginInvoke((MethodInvoker)delegate()
      {
        maskedTextBox3Terminal.Select(0, 0);
      });
    }

    //Auto moves caret on masked text box to start 3: Shit just got serious!
    private void maskedTextBox4Terminal_Enter(object sender, EventArgs e)
    {
      this.BeginInvoke((MethodInvoker)delegate()
      {
        maskedTextBox4Terminal.Select(0, 0);
      });
    }

    private void button1Inserir_Click(object sender, EventArgs e)
    {
      CreateData(1);
      this.Close();
    }

    private void button2Inserir_Click(object sender, EventArgs e)
    {
      CreateData(2);
      this.Close();
    }

    private void button3Inserir_Click(object sender, EventArgs e)
    {
      CreateData(3);
      this.Close();
    }

    private void button4Inserir_Click(object sender, EventArgs e)
    {
      CreateData(4);
      this.Close();
    }

    private void button9OK_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void Form2_KeyDown(object sender, KeyEventArgs e)
    {
      //TODO
      //Not working...? WTF
      if(e.KeyCode == Keys.Escape)
      {
        this.Close();
      }
    }
  }
}